﻿using HuiLian.Common.HdlHttpModel;
using HuiLian.Common.Helpers;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Service.HdlScada.AlarmRealTime;
using HuiLian.Service.Platform.CallTime;
using HuiLian.Web.Controllers.Platform;
using HuiLian.Web.Db;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace HuiLian.Web.Controllers.HdlScada
{
    /// <summary>
    /// 实时报警记录
    /// </summary>
    public class HdlScadaAlarmRealTimeController : AreaController
    {
        private readonly IHdlScadaAlarmRealTimeService _hdlScadaAlarmRealTimeServices;
        private readonly IHttpClientFactory _httpClientFactory = null!;
        private readonly HttpHelper _httpHelper;

        public HdlScadaAlarmRealTimeController(IHdlScadaAlarmRealTimeService hdlScadaAlarmRealTimeServices, HttpHelper httpHelper)
        {
            _hdlScadaAlarmRealTimeServices = hdlScadaAlarmRealTimeServices;
            _httpHelper = httpHelper;
        }

        /// <summary>
        /// 查询单条模型执行记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IResponseOutput> Get(long id)
        {
            return await _hdlScadaAlarmRealTimeServices.GetAsync(id);
        }

        /// <summary>
        /// 查询分页实时报警记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> GetPage(PageInput<HdlScadaAlarmRealTimeEntity> model)
        {
            return await _hdlScadaAlarmRealTimeServices.PageAsync(model);
        }

        /// <summary>
        /// 查询实时报警记录列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IResponseOutput> GetList()
        {            
            try
            {
                #region 定期刷新token

                //if (i % refreshToken == 0)
                //{
                //刷新token
                var result = await _httpHelper.RealmsZzssProtocolTokenResponseAsync();
                if (result.access_token == null)
                {
                    return ResponseOutput.NotOk("获取大数据对接的token为null");
                }
                GlobalVariable.RealmsZzssProtocolToken = result.access_token;

                System.Threading.Thread.Sleep(200);

                string client_id = "g2xmb";
                string client_secret = "ff914307";
                var access = await _httpHelper.HdlOauthAccessResponseAsync(client_id, client_secret);
                if (access.access_token == null)
                {
                    return ResponseOutput.NotOk("获取的token为null");
                }
                GlobalVariable.HdlOauthAccessToken = access.access_token;
                //}

                #endregion

                //获取实时报警记录                
                return await _hdlScadaAlarmRealTimeServices.HdlScadaAlarmRealtimeAsync();
            }
            catch(Exception ex)
            {                
                return await _hdlScadaAlarmRealTimeServices.GetListAsync();
            }            
        }
    }
}
